package com.katze.ssh.cmd;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Consumer;

public abstract class ExecCommand {

    protected String toCommand(String template, Map<String, String> variables) {
        int i = 0;
        StringBuilder result = new StringBuilder();
        while (i < template.length()) {
            int start = template.indexOf("${", i);
            if (start == -1) {
                result.append(template, i, template.length());
                break;
            }
            result.append(template, i, start);
            int end = template.indexOf("}", start);
            if (end != -1) {
                result.append(variables.getOrDefault(template.substring(start + 2, end), ""));
                i = end + 1;
            } else {
                result.append(template, start, template.length());
                break;
            }
        }
        return result.toString();
    }

    protected void resolve(InputStream in, int columns, Consumer<Properties> action) {
        try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8)){
            List<String> fields = new ArrayList<>();
            int last = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (fields.isEmpty()) {
                    if (columns == 0) {
                        // 第一行是列名
                        for (String item : line.split(" ")) {
                            if (item.isBlank()) continue;
                            fields.add(item.trim());
                        }
                        last = fields.size() - 1;
                        if (fields.contains("cmd") && fields.indexOf("cmd") != last) {
                            throw new IllegalStateException("CMD列必须在末尾");
                        }
                        continue;
                    } else {
                        for (int i = 1; i <= columns; i++) {
                            fields.add(String.valueOf(i));
                        }
                        last = fields.size() - 1;
                    }
                }
                Properties item = new Properties();
                for (int i = 0; i < fields.size(); i++) {
                    if (i != last) {
                        // 32 代表空格符
                        int index = line.indexOf(32);
                        item.put(fields.get(i), line.substring(0, index));
                        line = line.substring(index).trim();
                    } else {
                        item.put(fields.get(i), line);
                    }
                }
                action.accept(item);
            }
        }
    }

//    default void resolve(InputStream in, int columns, Consumer<Map<String, String>> action) {
//        try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8)){
//            while (scanner.hasNextLine()) {
//                String line = scanner.nextLine().trim();
//                Map<String, String> item = new HashMap<>();
//                for (int i = 1; i <= columns; i++) {
//                    if (i != columns) {
//                        int index = line.indexOf(32);
//                        item.put(String.valueOf(i), line.substring(0, index));
//                        line = line.substring(index).trim();
//                    } else {
//                        item.put(String.valueOf(i), line);
//                    }
//                }
//                action.accept(item);
//            }
//        }
//    }
}
